﻿using System;
using System.Linq;
using DebIt.Persistence;
using DebIt.Persistence.Model;

namespace DebIt.Business.Test
{
    public static class TestHelper
    {
        private const string ConnectionString = "Data Source=isostore:/ToDo.sdf";

        public static void InitializeTestData()
        {
            InsertClinets();
            InsertLoans();
            InsertLoanItems();
            InsertClientLoans();
        }

        private static void InsertClinets()
        {
            using (var context = new DebitDataContext(ConnectionString))
            {
                context.Clients.InsertOnSubmit(new ClientPo { FirstName = "Emma", LastName = "Smith", EMailAddress = "emma@smith.ch", Phonenumber = "12" });
                context.Clients.InsertOnSubmit(new ClientPo { FirstName = "Noah", LastName = "Johnson", EMailAddress = "noah@johnson.ch", Phonenumber = "34" });
                context.Clients.InsertOnSubmit(new ClientPo { FirstName = "Olivia", LastName = "Miller", EMailAddress = "olivia@miller.ch", Phonenumber = "56" });
                context.SubmitChanges();
            }
        }

        private static void InsertLoans()
        {
            using (var context = new DebitDataContext(ConnectionString))
            {
                context.Loans.InsertOnSubmit(new LoanPo { Currency = "CHF", Description = "supper by dieci", Timestamp = DateTime.UtcNow , Name = "Haway"});
                context.Loans.InsertOnSubmit(new LoanPo { Currency = "£", Description = "holiday in London", Timestamp = DateTime.UtcNow, Name = "Australia" });
                context.SubmitChanges();
            }
        }

        private static void InsertLoanItems()
        {
            using (var context = new DebitDataContext(ConnectionString))
            {
                var loan = context.Loans.Where(x => x.LoanId == 1).FirstOrDefault();
                if (loan != null)
                {
                    context.LoanItems.InsertOnSubmit(new LoanItemPo { Loan = loan, Description = "flight", Value = 250.55m });
                    context.LoanItems.InsertOnSubmit(new LoanItemPo { Loan = loan, Description = "hotel", Value = 123.55m });
                    context.LoanItems.InsertOnSubmit(new LoanItemPo { Loan = loan, Description = "shopping", Value = 98.31m });
                }

                loan = context.Loans.Where(x => x.LoanId == 2).FirstOrDefault();
                if (loan != null)
                {
                    context.LoanItems.InsertOnSubmit(new LoanItemPo { Loan = loan, Description = "to London", Value = 90.55m });
                    context.LoanItems.InsertOnSubmit(new LoanItemPo { Loan = loan, Description = "to Zürich", Value = 122.35m });
                }
                context.SubmitChanges();
            }
        }

        private static void InsertClientLoans()
        {
            using (var context = new DebitDataContext(ConnectionString))
            {
                var loan = context.Loans.Where(x => x.LoanId == 1).FirstOrDefault();
                var emma = context.Clients.Where(x => x.ClientId == 1).FirstOrDefault();
                var noah = context.Clients.Where(x => x.ClientId == 2).FirstOrDefault();
                var olivia = context.Clients.Where(x => x.ClientId == 3).FirstOrDefault();
                if (loan != null)
                {
                    if (emma != null)
                        context.LoanClients.InsertOnSubmit(new ClientLoanStatePo { Client = emma, Loan = loan, IsPaid = true, TimestampPaid = DateTime.UtcNow });
                    if (noah != null)
                        context.LoanClients.InsertOnSubmit(new ClientLoanStatePo { Client = noah, Loan = loan, IsPaid = false });
                    if (olivia != null)
                        context.LoanClients.InsertOnSubmit(new ClientLoanStatePo { Client = olivia, Loan = loan, IsPaid = false });
                }
                loan = context.Loans.Where(x => x.LoanId == 2).FirstOrDefault();
                if (loan != null)
                {
                    if (emma != null)
                        context.LoanClients.InsertOnSubmit(new ClientLoanStatePo { Client = emma, Loan = loan, IsPaid = true, TimestampPaid = DateTime.UtcNow });
                    if (noah != null)
                        context.LoanClients.InsertOnSubmit(new ClientLoanStatePo { Client = noah, Loan = loan, IsPaid = false });
                }
                context.SubmitChanges();
            }
        }
    }
}
